package com.huiztech.maystall.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;

public class HomeActivity extends ActivityGroup {

    private TabHost tabHost = null;
    private LayoutInflater mInflater = null;
    private ImageView animImageView;
    private AnimationDrawable anim;

    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tabs);

        mInflater = LayoutInflater.from(this);

        tabHost = (TabHost) findViewById(R.id.mytabhost);
        tabHost.setup(this.getLocalActivityManager());
        Intent intent;

        intent = new Intent(this, ShoppingCartActivity.class);
        View tab1Spec = mInflater.inflate(R.layout.tab1_spec, null);
        tab1Spec.setBackgroundColor(getResources().getColor(R.color.touming));
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tab1Spec).setContent(intent));

        intent = new Intent(this, ClassifyActivity.class);
        View tab2Spec = mInflater.inflate(R.layout.tab2_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(tab2Spec).setContent(intent));

        intent = new Intent(this, MainActivity.class);
        View tab3Spec = mInflater.inflate(R.layout.tab3_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(tab3Spec).setContent(intent));

        animImageView = (ImageView) tab3Spec.findViewById(R.id.tab_anim_image);
        animImageView.setBackgroundResource(R.anim.main_tab_image);
        anim = (AnimationDrawable) animImageView.getBackground();
        anim.setOneShot(true);

        intent = new Intent(this, MyInfoActivity.class);
        View tab4Spec = mInflater.inflate(R.layout.tab4_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator(tab4Spec).setContent(intent));

        intent = new Intent(this, MoreOptionsActivity.class);
        View tab5Spec = mInflater.inflate(R.layout.tab5_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator(tab5Spec).setContent(intent));

        int tabId = intent.getIntExtra("tabId", 2);
        tabHost.setCurrentTab(tabId);
        tabHost.setPadding(tabHost.getPaddingLeft(), tabHost.getPaddingTop(), tabHost.getPaddingRight(),
                tabHost.getPaddingBottom() - 5);

        tab3Spec.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tabHost.setCurrentTab(2);
                if (anim.isRunning()) {
                    anim.stop();// 停止
                }
                anim.start();// 启动
            }
        });

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出美淘", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(event.getKeyCode(), event);
    }

}
